const express = require("express");
const router = express.Router();

const pool = require("../../../src/pg/config/pg");
//查询所有的数据
router.get("/query", async (req, res) => {
    await pool.query("SELECT * FROM xiaobao  LIMIT 100", (err, resq) => {
      if (err) {
        return res.status(200).json({ id: "1", name: "获取后台数据失败" });
        pool.end;
      }
      res.status(200).json(resq.rows);
      pool.end;
    });
  });

//规则  统计
  router.get("/query/c", async (req, res) => {
    await pool.query("SELECT status, COUNT(*) as count FROM xiaobao WHERE status = '正常节点' GROUP BY status;", (err, resq) => {
      if (err) {
        return res.status(200).json(resq.rows);
        pool.end;
      }
      res.status(200).json(resq.rows);
      pool.end;
    });
  });
  router.get("/query/y", async (req, res) => {
    await pool.query("SELECT status, COUNT(*) as count FROM xiaobao WHERE status = '延迟节点' GROUP BY status;", (err, resq) => {
      if (err) {
        return res.status(200).json(resq.rows);
        pool.end;
      }
      res.status(200).json(resq.rows);
      pool.end;
    });
  });
  router.get("/query/d", async (req, res) => {
    await pool.query("SELECT status, COUNT(*) as count FROM xiaobao WHERE status = '掉线节点' GROUP BY status;", (err, resq) => {
      if (err) {
        return res.status(200).json(resq.rows);
        pool.end;
      }
      res.status(200).json(resq.rows);
      pool.end;
    });
  });
  router.get("/query/x", async (req, res) => {
    await pool.query("SELECT status, COUNT(*) as count FROM xiaobao WHERE status = '新作节点' GROUP BY status;", (err, resq) => {
      if (err) {
        return res.status(200).json(resq.rows);
        pool.end;
      }
      res.status(200).json(resq.rows);
      pool.end;
    });
    router.get("/query/z", async (req, res) => {
      await pool.query("SELECT COUNT(*) AS count  FROM xiaobao;", (err, resq) => {
        if (err) {
          return res.status(200).json(resq.rows);
          pool.end;
        }
        res.status(200).json(resq.rows);
        pool.end;
      });
    });
  });


module.exports = router